package com.d2.modules.system.category.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.d2.common.domain.Result;
import com.d2.modules.system.category.entity.Category;
import com.d2.modules.system.category.service.ICategoryService;
import com.d2.modules.system.dict.entity.Dict;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    @GetMapping("/children")
    public Result<List<Category>> loadChildren(Long id,@RequestParam(value = "exclude",defaultValue = "-1") Long exclude){
        Category q = new Category();
        q.setPid(id);
        LambdaQueryWrapper<Category> qw = Wrappers.lambdaQuery(q);
        qw.notIn(Category::getId, exclude);
        return Result.success(categoryService.list(qw));
    }

    @GetMapping("/parent")
    public Result<Category> getParent(Long id,@RequestParam(value = "exclude",defaultValue = "-1") Long exclude){
        Category category = categoryService.getById(id);
        return Result.success(categoryService.getById(category.getPid()));
    }

    @GetMapping("/tree")
    public Result<List<Category>> tree(){
        List<Category> tree = categoryService.tree(0L);
        return Result.success(tree);
    }

    @PostMapping("add")
    @Transactional
    public Result<Void> add(@RequestBody Category obj){
        Long pid = obj.getPid();
        if(pid != null && pid != 0){
            Category parent = categoryService.getById(pid);

            if(parent != null){
                parent.setHasChildren(1);
                categoryService.updateById(parent);
            }
        }
        categoryService.save(obj);
        return Result.success();
    }

    @PutMapping("update")
    public Result<Void> update(@RequestBody Category obj){
        Category category = categoryService.getById(obj.getId());
        Long pid1 = obj.getPid();
        Long pid = category.getPid();
        categoryService.updateById(obj);
        if(!Objects.equals(pid, pid1)) {
            categoryService.updateHasChildren(pid1);
            categoryService.updateHasChildren(pid);
        }
        return Result.success();
    }

    @GetMapping("/page")
    public Result<Page<Category>> page(Page<Category> page, Category query){
        query.setPid(0L);
        return Result.success(categoryService.page(page, new QueryWrapper<>(query)));
    }

    @GetMapping("{id}")
    public Result<Category> info(@PathVariable Long id){
        return Result.success(categoryService.getById(id));
    }

    @GetMapping("/list")
    public Result<List<Category>> list(Category query){
        query.setPid(0L);
        return Result.success(categoryService.list(new QueryWrapper<>(query)));
    }

    @DeleteMapping("/delete/{id}")
    public Result<Void> delete(@PathVariable Long id){
        categoryService.deleteById(id);
        return Result.success();
    }
}
